<?php
namespace Common\Model;
use Common\Model\BaseModel;
use Think\Model;

/**
 * Class MenuModel
 * @package Admin\Model
 * 后台菜单模型
 */
class MenuModel extends BaseModel {
    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'listorder desc';

    public function has_child($pid = 0){
        $map['parentid'] = array('eq', $pid);
        $map['display'] = 1;
        $one = $this->where($map)->find();
        return $one?$one:false;
    }

    public function has_child_all($pid = 0){
        $map['parentid'] = array('eq', $pid);
        $one = $this->where($map)->find();
        return $one?$one:false;
    }

    public function get_show_menu_by_pid($pid){
        $map['parentid'] = array('eq', $pid);
        $map['display'] = 1;
        $one = $this->where($map)->order($this->orderStr)->select();
        return $one?$one:false;
    }

    /**
     * @param int $pid
     * @return string
     * 返回从顶级开始的字符串
     */
    public function get_easyui_menu_tree_all($pid=0){
        $map['parentid'] = $pid;
//        $map['display'] = 1;
        $list = $this->where($map)->order($this->orderStr)->select();
        $str = '';
        foreach($list as $k => $v){
            $str .= '<li>';
            if($this->has_child_all($v['id'])){
                $str .= '<span><input type="hidden" class="menu" name="priv[]" value="'.$v['id'].'"/>'.LANG($v['name']).'</span>';
                $str .= '<ul>';
                $str .= $this->get_easyui_menu_tree_all($v['id']);
                $str .= '</ul>';
            }else{
                $str.= '<input type="hidden" class="menu" name="priv[]" value="'.$v['id'].'"/>'.LANG($v['name']);
            }
            $str .= '</li>';
        }
        return $str;
    }

    /**
     * @param int $pid
     * @return $return_array
     * 返回json格式数据
     * 返回从顶级开始的菜单,返回数组,全部菜单，用于权限设置
     */
    public function get_easyui_menu_tree_all_json($pid=0, $role_id){
        static $role_priv;
        if(!isset($role_priv)){
            $priv = D('RolePriv')->queryObj($role_id, 'role_id');
            $role_priv = $priv['role_pri'];
            $role_priv = explode(',', $role_priv);
            //unset($priv);
        }
        $return_array = array();
        $map['parentid'] = $pid;
        $list = $this->where($map)->order($this->orderStr)->select();

        foreach($list as $k => $v){
            $temp = array();
            $temp['id'] = $v['id'];
            $temp['text'] = LANG($v['name']);
            if(in_array($v['id'], $role_priv)&&!$this->has_child_all($v['id'])){
                $temp['checked'] = true;
				//echo $v['id'].',';
            }
            if($this->has_child_all($v['id'])){
                $temp['children'] = $this->get_easyui_menu_tree_all_json($v['id'], $role_id);
            }
            $return_array[] = $temp;
        }
        return $return_array;
    }

    /**
     * @param int $pid
     * @param $role_id
     * @return array
     * 返回查询菜单，用于页面左侧菜单更换
     */
    public function get_easyui_menu_tree_json($pid=0, $role_id, $index = 0){
        static $role_priv;
        if(!$role_priv){
            $priv = D('RolePriv')->queryObj($role_id, 'role_id');
            $role_priv = $priv['role_pri'];
            $role_priv = explode(',', $role_priv);
            unset($priv);
        }
        $return_array = array();
        $map['parentid'] = $pid;
        $map['display'] = 1;
        $list = $this->where($map)->order($this->orderStr)->select();
        foreach($list as $k => $v){
            if(!in_array($v['id'], $role_priv)){
               continue;
            }
            if($index == 0){

            }
            $temp = array();
            $temp['id'] = $v['id'];
            if($v['m']&&$v['c']&&$v['a'])
                $temp['text'] = '<a href="javascript:addTab(\''.LANG($v['name']).'\',\'/'.$v['m'].'/'.$v['c'].'/'.$v['a'].'\', '.$v['id'].')">'.LANG($v['name']).'</a>';
            else
                $temp['text'] = '<a href="javascript:addTab(\''.LANG($v['name']).'\',\'#\', '.$v['id'].')">'.LANG($v['name']).'</a>';
            if($this->has_child($v['id'])){
                $temp['children'] = $this->get_easyui_menu_tree_json($v['id'], $role_id, $index+1);
            }
            $return_array[] = $temp;
        }
        return $return_array;
    }

//    public function get_easyui_menu_tree_json($pid=0, $role_id){
//        static $role_priv;
//        if(!$role_priv){
//            $priv = D('RolePriv')->queryObj($role_id, 'role_id');
//            $role_priv = $priv['role_pri'];
//            $role_priv = explode(',', $role_priv);
//            unset($priv);
//        }
//        $return_array = array();
//        $map['parentid'] = $pid;
//        $map['display'] = 1;
//        $list = $this->where($map)->order($this->orderStr)->select();
//        foreach($list as $k => $v){
//            if(!in_array($v['id'], $role_priv)){
//                continue;
//            }
//            $temp = array();
//            $temp['id'] = $v['id'];
//            if($v['m']&&$v['c']&&$v['a'])
//                $temp['text'] = '<a href="javascript:addTab(\''.LANG($v['name']).'\',\'/'.$v['m'].'/'.$v['c'].'/'.$v['a'].'\', '.$v['id'].')">'.LANG($v['name']).'</a>';
//            else
//                $temp['text'] = '<a href="javascript:addTab(\''.LANG($v['name']).'\',\'#\', '.$v['id'].')">'.LANG($v['name']).'</a>';
//            if($this->has_child($v['id'])){
//                $temp['children'] = $this->get_easyui_menu_tree_json($v['id'], $role_id);
//            }
//            $return_array[] = $temp;
//        }
//        return $return_array;
//    }


    /**+
     * @param int $pid
     * @param $role_id
     * 手风琴样式
     */
    public function get_easyui_menu_accordion($pid=0, $role_id){
        //查询权限
        static $role_priv;
        if(!$role_priv){
            $priv = D('RolePriv')->queryObj($role_id, 'role_id');
            $role_priv = $priv['role_pri'];
            $role_priv = explode(',', $role_priv);
            unset($priv);
        }
        $return_array = array();
        $return_tr = '';
        $map['parentid'] = $pid;
        $map['display'] = 1;
        $list = $this->where($map)->order($this->orderStr)->select();
        foreach($list as $k => $v){
            if(!in_array($v['id'], $role_priv)){
                continue;
            }
            $return_tr = '';
            $temp['title'] = LANG($v['name']);
            $return_tr .= '<div title="'.LANG($v['name']).'" data-options="iconCls:\'icon_ext '.$v['icon'].'\'" >';
            $return_tr .= '<ul class="easyui-tree"  data-options="lines:true">';
            $t_map['parentid'] = $v['id'];
            $t_map['display'] = 1;
            $second_meun = $this->where($t_map)->order('listorder desc')->select();
            foreach($second_meun as $sk => $sv){
                if(!in_array($sv['id'], $role_priv)){
                    continue;
                }
                if($this->has_child($sv['id']))
                    $return_tr .= '<li iconCls="icon_ext '.$sv['icon'].'" data-options="state:\'closed\'">';
                else
                    $return_tr .= '<li iconCls="icon_ext '.$sv['icon'].'">';
                if($sv['m']&&$sv['c']&&$sv['a'])
                    $return_tr.= '<span><a href="javascript:addTab(\''.LANG($sv['name']).'\',\'/'.$sv['m'].'/'.$sv['c'].'/'.$sv['a'].'\', \'icon_ext '.$sv['icon'].'\', \''.$sv['id'].'\')">'.LANG($sv['name']).'</a></span>';
                else
                    $return_tr.= '<span iconCls="icon_ext '.$sv['icon'].'">'.LANG($sv['name']).'</span>';
                $tt_map['parentid'] = $sv['id'];
                $tt_map['display'] = 1;
                $third_meun = $this->where($tt_map)->order('listorder desc')->select();
                if($this->has_child($sv['id'])){
                    $return_tr .= '<ul>';
                    foreach($third_meun as $tk => $tv){
                        if(!in_array($tv['id'], $role_priv)){
                            continue;
                        }
                        if($this->has_child($tv['id']))
                            $return_tr .= '<li id="'.$tv['id'].'" iconCls="icon_ext '.$tv['icon'].'" data-options="state:\'closed\'">';
                        else
                            $return_tr .= '<li id="'.$tv['id'].'" iconCls="icon_ext '.$tv['icon'].'">';
                        if($tv['m']&&$tv['c']&&$tv['a'])
                            $return_tr.= '<span><a href="javascript:addTab(\''.LANG($tv['name']).'\',\'/'.$tv['m'].'/'.$tv['c'].'/'.$tv['a'].'\', \'icon_ext '.$tv['icon'].'\', \''.$tv['id'].'\')">'.LANG($tv['name']).'</a></span>';
                        else
                            $return_tr.= '<span iconCls="icon_ext '.$tv['icon'].'">'.LANG($tv['name']).'</span>';
                        if($this->has_child($tv['id'])){
                            $fo_map['parentid'] = $tv['id'];
                            $fo_map['display'] = 1;
                            $four_meun = $this->where($fo_map)->order('listorder desc')->select();
                            $return_tr .= '<ul>';
                            foreach($four_meun as $fk => $fv){
                                if(!in_array($fv['id'], $role_priv)){
                                    continue;
                                }
                                $return_tr .= '<li id="'.$fv['id'].'" iconCls="icon_ext '.$fv['icon'].'">';
                                if($fv['m']&&$fv['c']&&$fv['a'])
                                    $return_tr.= '<span><a href="javascript:addTab(\''.LANG($fv['name']).'\',\'/'.$fv['m'].'/'.$fv['c'].'/'.$fv['a'].'\', \'icon_ext '.$fv['icon'].'\', \''.$fv['id'].'\')">'.LANG($fv['name']).'</a></span>';
                                else
                                    $return_tr.= '<span iconCls="icon_ext '.$fv['icon'].'">'.LANG($fv['name']).'</span>';
                                $return_tr .= '</li>';
                            }
                            $return_tr .= '</ul>';
                        }
                        $return_tr .= '</li>';
                    }
                    $return_tr .= '</ul>';
                }

                $return_tr .= '</li>';
            }
            $return_tr .= '</ul>';
            $return_tr .= '</div>';
            $temp['content'] = $return_tr;
            $return_array[] = $temp;
        }
        return $return_array;
    }

    public function get_easyui_menu_tree(){
        $map['parentid'] = 0;
        $map['display'] = 1;
        $list = $this->where($map)->order($this->orderStr)->select();
        $role = get_module_session('Admin', 'user_role');
        $role_priv_model = D('RolePriv');
        $priv = $role_priv_model->query_role_priv($role);
        $str .= '<div class="easyui-accordion" data-options="fit:true,border:false">';
        foreach($list as $k => $v){
            if(!in_array($v['id'], $priv)){
                continue;
            }
            $str .= '<div title="'.LANG($v['name']).'" data-options="selected:false" style="overflow:auto;">';
            if($this->has_child($v['id'])){
                $str .= '<ul class="easyui-tree" data-options="lines:true">';
                $se_list = $this->get_show_menu_by_pid($v['id']);
                foreach($se_list as $sk => $sv){
                    if(!in_array($sv['id'], $priv)){
                        continue;
                    }
                    $str .= '<li>';
                    if($this->has_child($sv['id'])){
                        $str .= '<span>'.LANG($sv['name']).'</span>';
                        $str .= '<ul>';
                        $te_list = $this->get_show_menu_by_pid($sv['id']);
                        foreach($te_list as $tk => $tv){
                            if(!in_array($tv['id'], $priv)){
                                continue;
                            }
                            $str .= '<li>';
                            $str.= '<a href="javascript:addTab(\''.LANG($tv['name']).'\',\'/'.$tv['m'].'/'.$tv['c'].'/'.$tv['a'].'\', '.$tv['id'].')">'.LANG($tv['name']).'</a>';
                            $str .= '</li>';
                        }
                        $str .= '</ul>';
                    }else{
                        $str.= '<a href="javascript:addTab(\''.LANG($sv['name']).'\',\'/'.$sv['m'].'/'.$sv['c'].'/'.$sv['a'].'\', '.$sv['id'].')">'.LANG($sv['name']).'</a>';
                    }
                    $str .= '</li>';
                }
                $str .= '</ul>';
            }else{
                $str.= '<a onclick="javascript:addTab(\''.LANG($v['name']).'\',\'/'.$v['m'].'/'.$v['c'].'/'.$v['a'].'\', '.$v['id'].')">'.LANG($v['name']).'</a>';
            }
            $str .= '</div>';

        }
        $str .= '</div>';
        return $str;
    }

    public function query_top_menu(){
        $role = get_module_session('Admin', 'user_role');
        $role_priv_model = D('RolePriv');
        $priv = $role_priv_model->query_role_priv($role);
        $map['parentid'] = 0;
        $map['display'] = 1;
        $list = $this->where($map)->order($this->orderStr)->select();
        foreach($list as $k => $v){
            if(!in_array($v['id'], $priv)){
                continue;
            }
            $return_array[] = $v;
        }
        return $return_array;
    }

    /**
     * @param $data
     * @return bool
     * 保证添加的菜单是唯一的
     */
    public function add_menu($data){
        if(!isset($data['name']) || !$data['name']){
            return false;
        }
        $one = $this->queryObj($data['name'], 'name');
        if(!$one){
            return $this->add($data);
        }
        return false;
    }


    public function del_menu($name){
        if(!$name){
            return false;
        }
        $one = $this->queryObj($name, 'name');
        if(!$one){
            return false;
        }
        //同时删除权限
        $role = get_module_session('Admin', 'user_role');
        $role_priv = D('RolePriv')->queryObj($role, 'role_id');
        $role_priv_array = explode(',', $role_priv['role_pri']);
        foreach($role_priv_array as $key => $v){
            if($role_priv_array[$key] == $one['id'])
                unset($role_priv_array[$key]);
        }
        $role_priv['role_pri'] = implode(',', $role_priv_array);
        D('RolePriv')->save($role_priv);
        return $this->delete($one['id']);
    }

    public function query_menu_by_mca($module, $controller, $action){
        $map['m'] = array('eq', $module);
        $map['c'] = array('eq', $controller);
        $map['a'] = array('eq', $action);
        $one = $this->where($map)->find();
        return $one?$one:false;
    }

}